﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EventHub.Data.DataModel;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace EventHub.UI.ViewModel
{
    public enum EventStatus { Running, Completed, Failure, NotRun }
    public class SACBusinessEventViewModel : INotifyPropertyChanged
    {       
        readonly ReadOnlyCollection<SACBusinessEventViewModel> _childEvents;
        readonly SACBusinessEventViewModel _parentEvent;
        readonly SACBusinessEvent _businessEvent; 

        bool _isExpanded;
        bool _isSelected;

        public SACBusinessEventViewModel(SACBusinessEvent businessEvent) 
            : this(businessEvent, null)
        {
        }

        public SACBusinessEventViewModel(SACBusinessEvent businessEvent, SACBusinessEventViewModel parentEvent)
        {
            _businessEvent = businessEvent;
            _parentEvent = parentEvent;

            _childEvents = new ReadOnlyCollection<SACBusinessEventViewModel>(
                (from childEvent in _businessEvent.ChildEvents
                 select new SACBusinessEventViewModel(childEvent, this))
                        .ToList<SACBusinessEventViewModel>());
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        public string EventName
        {
            get
            {
                return _businessEvent.EventName;
            }
        }

        public EventStatus Status
        {
            get
            {
                return EventStatus.Completed;
            }
        }


        public ReadOnlyCollection<SACBusinessEventViewModel> ChildEvents
        {
            get
            {
                return _childEvents;
            }
        }

        public bool IsExpanded
        {
            get
            {
                return _isExpanded;
            }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    this.OnPropertyChanged("IsExpanded");
                }

                if (_isExpanded && _parentEvent != null)
                    _parentEvent.IsExpanded = true;
            }
        }

        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                if (value != _isSelected)
                {
                    _isSelected = value;
                    this.OnPropertyChanged("IsSelected");
                }
            }
        }

        public SACBusinessEventViewModel ParentEvent
        {
            get
            {
                return _parentEvent;
            }
        }

        public override bool Equals(object obj)
        {
            if (obj.ToString().Equals(this.EventName))
            {
                return true;
            }
            else
            {
                return false;
            }            
        }
    }
}
